package com.baidu.day5.Goods;

import java.util.ArrayList;

public class GoodServices {
    static ArrayList<Goods> goodsList = new ArrayList<>();

    static {
        Goods goods1 = new Goods(1, "手机", 1000.0, 100);
        Goods goods2 = new Goods(2, "电脑", 2000.0, 200);
        goodsList.add(goods1);
        goodsList.add(goods2);
    }

    //静态代码块 在类加载的时候执行一次,这个括号里面的内容 外面访问不到
    //上面那个静态变量,都可以访问
    public void findAll() {
        System.out.println("商品编号\t商品名称\t商品库存");
        for (Goods goods : goodsList) {
            System.out.println(goods.getNum() + "\t" + "\t"+  goods.getName() + "\t\t"+ "\t"+  + goods.getStock());
        }
    }

    //添加商品的功能
    public static boolean addGoods(Goods goods) {
        for (Goods goods1 : GoodServices.goodsList) {
            if (goods1.getNum().equals(goods.getNum()) || goods1.getName().equals(goods.getName())) {
                System.out.println("商品编号重复");
                return false;
            }
        }
        return goodsList.add(goods);
    }

    //删除商品的功能
    public static void deleteGoods(Integer num) {
        for (int i = 0; i < goodsList.size(); i++) {
            if (goodsList.get(i).getNum().equals(num)) {
                goodsList.remove(i);
                System.out.println("删除成功");
                return;
            }

        }
        System.out.println("没有找到该商品");
    }

}
